﻿/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Alexandre Croiseaux
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.events
{
	import flash.events.Event;
	import marcel.external.FileLoader;
	
	/**
	 * Event dispatched by the FileLoader and the FileLoaderQueue classes
	 * @author Alexandre Croiseaux
	 * @see marcel.external.FileLoader
	 * @see marcel.external.FileLoaderQueue
	 */
	public final class FileLoaderEvent extends Event
	{
		//--------------------------------------------------------------------------
		//
		//  Public vars
		//
		//--------------------------------------------------------------------------
		/**
		 * The content of the FileLoader instance dispatching the event
		 * defined if the current type is PROGRESS or COMPLETE
		 */
		public var content:*;
		
		/**
		 * The percent loaded of the FileLoader instance dispatching the event (0 -> 1)
		 * defined if the current type is PROGRESS
		 */
		public var percent:Number;
		
		/**
		 * The errorID of the FileLoader instance dispatching the event
		 * defined if the current type is ERROR
		 */
		public var errorID:int;
		
		/**
		 * The httpStatus code of the FileLoader instance dispatching the event
		 * defined if the current type is HTTP_STATUS
		 */
		public var httpStatus:int;
		
		/**
		 * The text error of the FileLoader instance dispatching the event
		 * defined if the current type is ERROR
		 */
		public var text:String;
		
		/**
		 * The bytesLoaded of the FileLoader instance dispatching the event
		 * defined if the current type is PROGRESS
		 */
		public var bytesLoaded:Number;
		
		/**
		 * The bytesTotal of the FileLoader instance dispatching the event
		 * defined if the current type is PROGRESS
		 */
		public var bytesTotal:Number;
		
		
		//--------------------------------------------------------------------------
		//
		//  Private vars
		//
		//--------------------------------------------------------------------------
		private var _fileLoader:FileLoader;
		
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Constructs a new FileLoaderEvent instance
		 * @param	type	the event type, must be a constant if this class
		 * @param	content	the loaded content
		 * @param	prc	the percent loaded (0 -> 1)
		 */
		public function FileLoaderEvent(type:String, content:* = null, prc:Number = 0)
		{
			super(type, false, false);
			this.content = content;
			percent = prc;
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Public methods
		//
		//--------------------------------------------------------------------------
		/**
		 * the FileLoader dispatching the event
		 */
		public function get fileLoader():FileLoader
		{
			if (_fileLoader == null) return target as FileLoader;
			return _fileLoader;
		}
		public function set fileLoader(dl:FileLoader):void { _fileLoader = dl; }
		
		
		/**
		 * overriding clone method for event redispatching
		 * @return	a copy of the current event
		 */
		override public function clone():Event
		{
			var evt:FileLoaderEvent = new FileLoaderEvent(type, content, percent);
			evt.errorID = errorID;
			evt.httpStatus = httpStatus;
			evt.bytesLoaded = bytesLoaded;
			evt.bytesTotal = bytesTotal;
			evt.fileLoader = fileLoader;
			evt.text = text;
			return evt;
		}
		
		/**
		* Dispatched when data has loaded successfully. The complete event is always dispatched after the init event.
		* @eventType	complete
		*/
		static public const COMPLETE:String = "complete";
		
		/**
		* Dispatched when data is received as the download operation progresses.
		* @eventType progress
		*/
		static public const PROGRESS:String = "progress";
		
		/**
		* Dispatched when an input or output error occurs that causes a load operation to fail.
		* @eventType ioError
		*/
		static public const ERROR:String = "ioError";
		
		/**
		* Dispatched when the properties and methods of a loaded SWF file are accessible
		* @eventType init
		*/
		static public const INIT:String = "init";
		
		/**
		* Dispatched when a network request is made over HTTP and an HTTP status code can be detected.
		* @eventType httpStatus
		*/
		static public const HTTP_STATUS:String = "httpStatus";
		
		/**
		* Dispatched when all files in the queue have been loaded (successfully or not).
		* @eventType onFileLoaderAllComplete
		*/
		static public const ALL_COMPLETE:String = "onFileLoaderAllComplete";
	}
}
